#pragma once
#include "Log.hpp"
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
class Util
{
public:
    static int RevcLine(int sock, std::string* out){
        out->clear();
        char c = '\0';
        while(c != '\n'){
            int s = recv(sock, &c, 1, 0);
            if(s > 0){
                if(c == '\r'){
                    recv(sock, &c, 1, MSG_PEEK);
                    if(c == '\n'){
                        recv(sock, &c, 1, 0);
                    }
                    else{
                        c = '\n';                    
                    }
                }
                out->push_back(c);
            }
            else if(s == 0){
                return 0;
            }
            else{
                LOG(WARNING, "RecvLine Error!");
                return -1;
            }
        }
        return out->size();
    }

    static bool CutString(const std::string& target, std::string* out1, std::string* out2, const std::string& sep){
        size_t pos = target.find(sep);
        if(pos == std::string::npos){
            return false;
        }
        *out1 = target.substr(0, pos);
        *out2 = target.substr(pos + sep.size());
        return true;
    }
};